﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using MySql.Data.MySqlClient;

using System;
using System.Data;
using System.Data.Linq.Mapping;
using System.Diagnostics;
using System.Reflection;
using DbLinq.Data.Linq;


namespace testemployeemvc.Models
{
    public class UserDataContext : DbLinq.MySql.MySqlDataContext
    {
        #region Validation

        public UserDataContext()
            : base(new MySqlConnection(ConfigurationManager.ConnectionStrings["mysiteConnectionString"].ConnectionString))
        {
        }


        public IEnumerable<User> GetUsers()
        {
            using (UserDataContext context = new UserDataContext())
            {
                var results = from s in context.User
                             orderby s.user_id ascending
                              select s;

                return results.Take(25).ToList();
            }
        }
        /*

        public User deleteUser(int id_pregunta)
        {
            User obj = null;
            try
            {
                User context = new User();
                obj = (from r in context
                       where r.id_pregunta.Equals(id_pregunta)
                       select r).First();
                if (obj != null)
                {
                    context.preguntas.DeleteOnSubmit(obj);
                    context.SubmitChanges();
                }
                return obj;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return obj;
            }
        }

        public User createUser(users pregunta)
        {
            try
            {
                employeetestDataContext context = new employeetestDataContext();
                context.registrar_preguntas(pregunta.id_usuario, users.pregunta1);
                //context.preguntas.InsertOnSubmit(users);
                context.SubmitChanges();
                return users;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return users;
            }
        }

        public User detailPregunta(int id)
        {

            users obj = null;
            try
            {
                employeetestDataContext context = new employeetestDataContext();
                obj = (from r in context.preguntas
                       where r.id_pregunta.Equals(id)
                       select r).First();

                return obj;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return obj;
            }

        }

        public User updatePregunta(User pregunta_update)
        {
            User obj = null;
            try
            {
                employeetestDataContext context = new employeetestDataContext();

                obj = (from r in context.preguntas
                       where r.id_pregunta.Equals(pregunta_update.user_id)
                       select r).First();

                obj.user_id = pregunta_update.user_id;
                obj.name=pregunta_update.name;

                context.SubmitChanges();
                return obj;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return obj;
            }

        }
         **/

        public DbLinq.Data.Linq.Table<User> User
        {
            get
            {
                return this.GetTable<User>();
            }
        }

    }
        #endregion
}

